/*
 * Copyright (c) 2023. All rights reserved.
 * Please don't try to modify the code, because I don't know what the consequences will be.
 *
 *
 *
 *
 *
 *
 *
 */

package cc.zzone.bean;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 服务配置类
 *
 * @author jinzhengze
 * @since 2023年03月03日
 */
@Slf4j
public abstract class ServerInfo {

    private int port = 221221;

    private long readerIdleTime;

    private TimeUnit timeUnit;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public long getReaderIdleTime() {
        return readerIdleTime;
    }

    public void setReaderIdleTime(long readerIdleTime) {
        this.readerIdleTime = readerIdleTime;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public void setTimeUnit(TimeUnit timeUnit) {
        this.timeUnit = timeUnit;
    }

    public abstract List<ChannelHandler> getHandlers();

    protected static void onlineLog(ChannelHandlerContext context) {
        Map<String, Object> info = new LinkedHashMap<>();
        info.put("channelId", context.channel().id().asLongText());
        info.put("url", context.channel().remoteAddress());
        log.info("Online         : {}", info);
    }

    protected static void heartTimeoutLog(ChannelHandlerContext context) {
        Map<String, Object> info = new LinkedHashMap<>();
        info.put("channelId", context.channel().id().asLongText());
        info.put("url", context.channel().remoteAddress());
        log.error("Heart Timeout  : {}", info);
    }

    protected static void readLog(ChannelHandlerContext context, Object msg) {
        Map<String, Object> info = new LinkedHashMap<>();
        info.put("channelId", context.channel().id().asLongText());
        info.put("url", context.channel().remoteAddress());
        info.put("message", msg);
        log.info("Get Msg        : {}", info);
    }

    protected static void removedLog(ChannelHandlerContext context) {
        Map<String, Object> info = new LinkedHashMap<>();
        info.put("channelId", context.channel().id().asLongText());
        info.put("url", context.channel().remoteAddress());
        log.info("Offline        : {}", info);
    }

    protected static void exceptionLog(ChannelHandlerContext context, Throwable cause) {
        Map<String, Object> info = new LinkedHashMap<>();
        info.put("channelId", context.channel().id().asLongText());
        info.put("url", context.channel().remoteAddress());
        info.put("throwable", cause);
        log.error("Error          : {}", info);
    }
}
